package musicstore.com.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import musicstore.com.dao.GenreDao;
import musicstore.com.dao.UserDao;
import musicstore.com.dao.impl.GenreDaoImpl;
import musicstore.com.dao.impl.UserDaoImpl;
import musicstore.com.entity.Genre;
import musicstore.com.entity.User;

public class LoginServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		GenreDao genreDao = new GenreDaoImpl();
		List<Genre> genreList = genreDao.getGenreList();
		request.setAttribute("genre", genreList);
		request.getRequestDispatcher("login.jsp").forward(request, response);

	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

			String username = request.getParameter("username");
			String password = request.getParameter("password");
			UserDao dao = new UserDaoImpl();
			User user = dao.checkUser(username, password);
			GenreDao genreDao = new GenreDaoImpl();
			List<Genre> genreList = genreDao.getGenreList();
			request.setAttribute("genre", genreList);
			if(user!=null){
				request.getSession().setAttribute("user", user.getUserName());
				response.sendRedirect("AlbumServlet");
			}else{
				request.setAttribute("error", "用户名或者密码错误");
				RequestDispatcher dispatcher = request.getRequestDispatcher("login.jsp");
				dispatcher.forward(request, response);
			}
	}

}
